package com.gem.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gem.domain.ClassInfoAPI;
import com.gem.domain.Student;
import com.gem.service.ClassInfoService;
import com.gem.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 玲
 * @Description: 考试平台调用的班级接口
 * @create 2019-11-06 14:27
 * @Modified By:
 */
@Controller
@SuppressWarnings("all")
public class ClassAPIController {

    @Autowired
    private ClassInfoService service;


    @RequestMapping("/factory/deleteClass")
    @ResponseBody
    public Map<Integer, Boolean> deleteById(@RequestBody String id, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");//同意哪些url能够跨域请求到本域
        response.setHeader("Access-Control-Allow-Methods", "POST"); //同意的请求方法，通常是GET,POST,PUT,DELETE,OPTIONS
        response.setHeader("Access-Control-Allow-Headers", "application/json;charset=UTF-8");
        Map<Integer, Boolean> map = new HashMap<>();
        System.out.println("======链接成功=====");
        System.out.println("id-->"+ id);
        try {
            if (service.deleteById(Integer.parseInt(id))) {
                map.put(0, true);
            } else {
                map.put(0, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put(0, false);
        }
        return map;
    }

    @RequestMapping("/factory/addClass")
    @ResponseBody
    public Map<Integer, Boolean> save(@RequestBody ClassInfoAPI classinfo, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");//同意哪些url能够跨域请求到本域
        response.setHeader("Access-Control-Allow-Methods", "POST"); //同意的请求方法，通常是GET,POST,PUT,DELETE,OPTIONS
        response.setHeader("Access-Control-Allow-Headers", "application/json;charset=UTF-8");
        Map<Integer, Boolean> map = new HashMap<>();
        System.out.println("======addClass链接成功=====");
        System.out.println("json-->"+classinfo);
        System.out.println(classinfo.getM_name());
        try {
            if (service.save(classinfo)) {
                map.put(0, true);
            } else {
                map.put(0, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put(0, false);
        }
        return map;
    }

    @RequestMapping("/factory/updateClass")
    @ResponseBody
    public Map<Integer, Boolean> update(@RequestBody ClassInfoAPI classinfo, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");//同意哪些url能够跨域请求到本域
        response.setHeader("Access-Control-Allow-Methods", "POST"); //同意的请求方法，通常是GET,POST,PUT,DELETE,OPTIONS
        response.setHeader("Access-Control-Allow-Headers", "application/json;charset=UTF-8");
        Map<Integer, Boolean> map = new HashMap<>();
        System.out.println("======updateClass链接成功=====");
        System.out.println("json-->"+classinfo);
        System.out.println(classinfo.getM_name());
        try {
            if (service.update(classinfo)) {
                map.put(0, true);
            } else{
                map.put(0, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put(0, false);
        }
        return map;
    }


    @RequestMapping("/factory/TestJson")
    @ResponseBody
    public Map<Integer,Boolean> test(@RequestBody String sId,HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");//同意哪些url能够跨域请求到本域
        response.setHeader("Access-Control-Allow-Methods", "POST"); //同意的请求方法，通常是GET,POST,PUT,DELETE,OPTIONS
        response.setHeader("Access-Control-Allow-Methods", "GET");
        response.setHeader("Access-Control-Allow-Headers", "application/json;charset=UTF-8");
        System.out.println(sId);
        Map<Integer,Boolean> map = new HashMap<>();
        map.put(1,true);
        return map;
    }

}
